package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @ClassName TapPaneDemo
 * @Description
 * @Author wangss
 * @date 2020.05.19 00:54
 * @Version 1.0
 */
public class TapPaneDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #07ddff");

        TabPane tabPane = new TabPane();
        tabPane.setStyle("-fx-background-color: #f4fffe");
        tabPane.setPrefSize(300d, 300d);
        Tab tab1 = new Tab("tab1");
        Tab tab2 = new Tab("tab2");
        Tab tab3 = new Tab("tab3");
        tabPane.getTabs().addAll(tab1, tab2, tab3);

        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.setStyle("-fx-background-color: #fff2f2");
        hBox.getChildren().addAll(new Button("button1"), new Button("button2"));
        tab1.setContent(hBox);

        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        vBox.setStyle("-fx-background-color: #dfe7ff");
        vBox.getChildren().addAll(new Button("button1"), new Button("button2"));
        tab2.setContent(vBox);

        Image image = new Image("image/icon1.png", 30d, 30d, true, true);
        tab1.setGraphic(new ImageView(image));

        // 取消关闭
        tab2.setClosable(false);

        // 默认选择
        tabPane.getSelectionModel().select(tab3);
        // 设置朝向
//        tabPane.setSide(Side.RIGHT);
        // 设置图片朝向，不跟随tab转换方向，有一个问题：图片必须放在show前边
//        tabPane.setRotateGraphic(false);

        anchorPane.getChildren().addAll(tabPane);

        Scene scene = new Scene(anchorPane);

        primaryStage.setScene(scene);
        primaryStage.setTitle("菜单");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        // 监听
        tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
            @Override
            public void changed(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue) {

            }
        });

        tab1.setOnSelectionChanged(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {

            }
        });

        tab1.setOnClosed(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {

            }
        });
    }
}
